package com.nota.flink.cdc.utils;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.nota.flink.cdc.exception.GlobalExceptionHandler;
import com.nota.flink.cdc.pojo.constants.RedisStreamConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 胡超雄
 * @date 2024/01/16 10:01
 * @desc
 */
@Slf4j
@Component
public class CommonUtil {

    @Resource
    private RedisUtil redisUtil;

    /**
     * 处理错误 ,1 打印错误日志 2 死信队列 3 发送钉钉
     */
    public void errorHandler(String errMessage, String jsonStr, String tempMessage, Exception e) {
        try {
            String exceptionMsg = "";
            if (e != null) {
                exceptionMsg = e.getMessage();
            }
            String uuid = UUID.fastUUID().toString();
            log.error("消息id:{},错误类型:{},数据实体:{},,错误消息:{}", uuid, errMessage, jsonStr, exceptionMsg);
            redisUtil.setCacheMapValue(RedisStreamConstants.DEAD_QUEUE + "canal_data",
                    uuid, jsonStr);
            String format = StrUtil.format(tempMessage, uuid, jsonStr);
            SpringUtil.getBean(DingUtils.class).sendToDing(GlobalExceptionHandler.TITLE, format);
        } catch (Exception ee) {
            log.error("处理打印日志出错,{},{},{}", errMessage, jsonStr, tempMessage, ee);
        }
    }
}
